package com.bwc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwc.common.R;
import com.bwc.domain.Category;
import com.bwc.domain.Dish;
import com.bwc.domain.DishFlavor;
import com.bwc.dto.DishDto;
import com.bwc.service.CategoryService;
import com.bwc.service.DishFlavorService;
import com.bwc.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //如果name为空,默认是接受到了一个null

        Page<Dish> pageInfo = new Page<>(page,pageSize);
        // 由于返回给前端页面的的数据不是单纯的dish，所欲要用到dto
        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);

        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,lambdaQueryWrapper);

        //将pageInfo的内容拷贝给dishDtoPage,我们这里是除去了records这个属性的copy
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> dishList = pageInfo.getRecords();
        List<DishDto> dishDtoList = dishList.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = dishDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoList);

       return R.success(dishDtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        dishService.saveWithFlavor(dishDto);

        return R.success("成功添加菜品");


    }

    @GetMapping("{id}")
    public R<DishDto> getDishDtoById(@PathVariable long id){

        DishDto dishDto = dishService.getDtoById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    @DeleteMapping
    public R<String> delete(Long[] ids){

        dishService.removeByIdWithFlavor(ids);
        return R.success("成功删除菜品消息");
    }

    @PostMapping("/status/0")
    public R<String> updateStatusStop(@RequestParam List<Long> ids){

        //update dish set status = 0 where id in ids
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids);

//        List<Dish> dishList = dishService.get(lambdaQueryWrapper);

        List<Dish> dishList = dishService.listByIds(ids);
        dishList = dishList.stream().map((item)->{
            item.setStatus(0);
            return item;
        }).collect(Collectors.toList());

        dishService.updateBatchById(dishList);
        return R.success("成功停售菜品");
    }

    @PostMapping("/status/1")
    public R<String> updateStatusStrat(Long[] ids){
//        for(Long id:ids){
//            Dish dish = dishService.getById(id);
//            dish.setStatus(1);
//            dishService.updateById(dish);
//        }
        LambdaUpdateWrapper<Dish> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Dish::getId,ids);
        lambdaUpdateWrapper.set(Dish::getStatus,1);
        dishService.update(lambdaUpdateWrapper);

        return R.success("成功起售菜品");
    }

//    @GetMapping("/list")
//    public R<List<Dish>> getByCategoryId(Dish dish){
//        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
//        lambdaQueryWrapper.eq(Dish::getStatus,1);
//        List<Dish> dishList=dishService.list(lambdaQueryWrapper);
//        return R.success(dishList);
//    }
        @GetMapping("/list")
        public R<List<DishDto>> getByCategoryId(Dish dish){

            LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
            lambdaQueryWrapper.eq(Dish::getStatus,1);
            List<Dish> dishList=dishService.list(lambdaQueryWrapper);
            List<DishDto> dishDtoList = dishList.stream().map((item)->{
                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,item.getId());
                //sql:select * from dish_flavor where dish_id = ?
                List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
                DishDto dishDto = new DishDto();
                BeanUtils.copyProperties(item,dishDto);
                dishDto.setFlavors(dishFlavorList);
                return dishDto;
            }).collect(Collectors.toList());

            return R.success(dishDtoList);

        }

}
